<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_edit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>

<body>
    <div class="nav">
        <img src="image/image1.jpg" alt="">
        <span class="title">个人博客系统</span>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="logout" onclick='logout()'>注销</a>
    </div>

    <div class="blog-edit-container">
        <div class="title">
            <input type="text" id="title" name="title">
            <button id="submit" onclick="updateBlog()">发布文章</button>
        </div>
        <div id="editor">
            <textarea name="content" style="display: none;">
                </textarea>
        </div>

    </div>

    <script>
        var blogId = getURLParam("blogId");
        if (blogId != null && blogId > 0) {
            loginState();
            getBlogDetail();
        } else {
            alert("非法参数！");
            location.href = 'blog_list.html'
        }
        function getBlogDetail() {
            jQuery.ajax({
                url: "blog/getblog",
                type: "POST",
                data: { "blogId": blogId },
                success: function (result) {
                    if (result != null && result.success == 200 && result.data != null
                        && result.data.userId > 0) {
                        jQuery("#title").val(result.data.title);
                        initEdit(result.data.content);
                    } else {
                        alert("数据加载失败，请重试！");
                    }
                }
            });
        }
        function initEdit(md) {
            editor = editormd("editor", {
                width: "100%",
                height: "calc(100% - 50px)",
                markdown: md,
                path: "editor.md/lib/",
                saveHTMLToTextarea: true
            });
        }
        function updateBlog() {
            var title = jQuery("#title");
            var content = jQuery("#content");
            if (jQuery.trim(title.val()) == "") {
                alert("请先输入标题！");
                title.focus();
                return;
            }
            if (jQuery.trim(content.val()) == "") {
                alert("请先输入正文！");
                content.focus();
                return;
            }
            jQuery.ajax({
                url: 'blog/update',
                type: "POST",
                data: {
                    "blogId": blogId,
                    "title": title.val(),
                    "content": content.val()
                },
                success: function (result) {
                    if (result != null && result.success == 200 && result.data == 1) {
                        alert("恭喜：修改成功！");
                        location.href = "blog_list.html";
                    } else {
                        alert("操作失败，请重试！")
                    }
                }
            });
        }
        function loginState() {
            jQuery.ajax({
                url: 'user/loginstate',
                type: 'GET',
                success: function (result) {
                    if (result != null && result.success == 200) {
                        if (result.data != null && result.data.userId > 0) {
                        } else {
                            alert("抱歉：请先登录！");
                            location.href = "login.html";
                        }
                    } else {
                        alert("抱歉：后端程序不可用，请您稍后再试！");
                    }
                }
            })

        }


        function getURLParam() {
            let url = location.search;
            let p = url.split('?')[1]
            let params = new URLSearchParams(p)
            return params.get('blogId'); //1
        }
        function logout() {
            if (confirm("是否退出？")) {
                jQuery.ajax({
                    url: "user/logout",
                    type: "POST",
                    success: function (result) {
                        if (result != null && result.success == 200 && result.data == 1) {
                            alert("退出成功！");
                            location.href = "index.html";
                        } else {
                            alert("抱歉：操作出错，请重试！");
                        }
                    }
                });
                l
            }
        }
    </script>
</body>

</html>